<template>
	<view class="au-voice" :class="{inverse : inverse}" @tap="onPlay">
		<view class="iconfont icon-icon_play"></view>
		<view class="au-bg" :class="{'au-voicePlay' :isPlay}">
			
		</view>
		<view class="au-ms">
			<text v-if="time!=0">{{time}}″</text>
		</view>
	</view>
</template>

<script>
	const innerAudioContext = uni.createInnerAudioContext(); //播放
	export default {
		name: "playVoice",
		props: {
			src: {
				type: [String],
				default: ''
			},
			time: {
				type: [Number],
				default: 0
			},
			inverse: {
				default: false
			}
		},
		created() {
			innerAudioContext.src = this.src;
		},
		data() {
			return {
				isPlay: false //正在播放
			};
		},
		methods: {
			onPlay: function(e) {
				if (this.isPlay) {
					this.isPlay = false;
					innerAudioContext.stop();
				} else {
					innerAudioContext.play();
					this.isPlay = true;
					innerAudioContext.onEnded(() => {
						this.isPlay = false;
					})
				}
			},
		}
	}
</script>

<style lang="scss" scoped>
	.au-voice {
		display: block;
		padding-top: 5px;
		padding-left: 10px;
		height: 80upx;
		width: 300upx;
		background: #3385ff;
		border-radius: 5px;
		position: relative;
	}

	.iconfont {
		position: absolute;
		top: 50%;
		left: -8px;
		margin-top: -16px;
		font-size: 16px;
		color: #3385ff;
		transform: rotate(180deg);
	}

	.au-bg {
		background: url() right 0 no-repeat;
		width: 17px;
		height: 17px;
		background-size: 350%;
		color: transparent;
		margin-top: 14upx;
		background-position: 100%;
	}

	.au-voicePlay {
		animation-name: voicePlay;
		animation-duration: 1s;
		animation-direction: normal;
		animation-iteration-count: infinite;
		animation-timing-function: steps(3);
	}

	.au-ms {
		position: absolute;
		right: 10px;
		color: #fff;
		top: 24upx;
		font-size: 12px;
	}

	@keyframes voicePlay {
		0% {
			background-position: 0;
		}

		100% {
			background-position: 100%;
		}
	}

	.au-voice.inverse {
		.iconfont {
			left: auto;
			right: -8px;
			transform: rotate(0);
		}
		.au-ms{
			right: auto;
			left: 10px;
		}
		.au-bg{
			transform: rotate(180deg);
			position: absolute;
			right: 10px;
		}

	}
</style>
